package com.nip.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nip.common.Constant;
import com.nip.miniproject_02.R;

/**
 * Main activity
 * 
 * @author NIP-ThangNM
 * 
 */
public class MainActivity extends Activity {

	private EditText edText;
	private Button btnSend;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		edText = (EditText) findViewById(R.id.edText);
		btnSend = (Button) findViewById(R.id.btnSendToB);

		btnSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplication(), AcvivityResult.class);
				i.putExtra(Constant.SEND_CODE_01, edText.getText().toString());
				startActivityForResult(i, Constant.REQUEST_CODE_01);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == Constant.REQUEST_CODE_01 && resultCode == Constant.RESULT_CODE_01)
		{
			String text = data.getStringExtra(Constant.SEND_CODE_02);
			edText.setText(text);
		}

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_activity, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int index = item.getItemId();
		switch (index) {
		case R.id.menu_exit:
			
			AlertDialog.Builder b = new AlertDialog.Builder(this);
			b.setTitle("NIPTeam");
			b.setMessage("Giờ Ý chú là sao ?");
			b.setPositiveButton("Thoát Thôi !", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			
			b.setNegativeButton("Đéo thoát nữa !", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			
			b.show();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
